package com.yanqu.road.web.controller.usercenter;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.servercenter.PackageMark;
import com.yanqu.road.entity.web.HttpGameWebParam;
import com.yanqu.road.server.manger.PackageVersionMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.web.controller.BaseGameHttpServlet;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/languagePackage")
public class LanguagePackage extends BaseGameHttpServlet {

	private static Logger logger = LogManager.getLogger(LanguagePackage.class.getName());

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/**
		 * 参数解析
		 */
		logger.info("request languagePackage");
		HttpGameWebParam httpGameWebParam = super.parseRequest(request);
		JSONObject paramObj = httpGameWebParam.getParamObj();
		String param = httpGameWebParam.getParam();
		String language = httpGameWebParam.getLanguage();

		String packageMark;
		try {
			packageMark = paramObj.getString("packageMark");
		} catch (Exception e) {
			sendMessage(response, getResultObj(GameErrorCode.E_REQUEST_ARGUMENT_ERROR, language).toJSONString());
			logger.error("languagePackage request json格式出错！ json 字符串param为{}", param, e);
			return;
		}

		PackageMark packageMarkData = PackageVersionMgr.getPackageMark(packageMark);
		if(null == packageMarkData){
			sendMessage(response, getResultObj(GameErrorCode.E_PACKAGE_MARK_NO_FOUND, language).toJSONString());
			logger.error("languagePackage packageMark no find, packageMark:{}", packageMark);
			return;
		}

		JSONObject resultObj = getResultObj(0, language);
		resultObj.put("languagePackageUrl", packageMarkData.getHotPackDownloadUrl());

		sendMessage(response, resultObj.toJSONString());
	}
}
